///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.data.vo {
	import sugarcookie.data.handlers.xml.XHTMLHandler;
	import sugarcookie.data.vo.Anchor;
	import sugarcookie.data.vo.Img;
	
	/**
	 * Value Object used when parsing XHTML.
	 */
	public class Paragraph extends Object {
		private var _xml:XML;
		private var _title:String;
		private var _anchors:Array = [];
		private var _images:Array = [];
		private var _html:String;
		private var _index:uint;
        
        public function Paragraph(_p:XML,_i:uint=0) {
        	_xml = _p;
        	_index = _i;
        	parse(_p);
        }
        
        private function parse(_p:XML):void {
        	var i:int;
        	
        	_title = _p.@title;
        	
        	var aList:XMLList = _p.a;
        	for (i=0;i<aList.length();i++) {
        		var anchor:Anchor = new Anchor(aList[i],i);
        		_anchors[anchor.title] = anchor;
        		
        		_p.replace(_p.a[i].childIndex(),XML(anchor.html));
        	}
        	
        	var iList:XMLList = _p.img;
        	for (i=0;i<iList.length();i++) {
        		var img:Img = new Img(iList[i]);
        		_images[img.title] = img;
        	}
        	
        	_html = XHTMLHandler.cleanHTML(_p.children());
        }
        
        public function get xml():XML {
			return _xml;
		}
		
		public function get title():String {
			return _title;
		}
		
		public function get index():uint {
			return _index;
		}
		
		public function get html():String {
        	return _html;
        }
        
        public function set html(html:String):void {
        	_html = html;
        }
        
        public function set anchors(a:Array):void {
        	_anchors = a;
        }
        
        public function get anchors():Array {
        	return _anchors;
        }
        
        public function set images(i:Array):void {
        	_images = i;
        }
        
        public function get images():Array {
        	return _images;
        }
	}
}